package org.ccheng.xztx.service.impl;

import cn.hutool.core.util.ReUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import org.ccheng.xztx.service.TrumpService;
import org.ccheng.xztx.util.Api;
import org.ccheng.xztx.util.ApiConstants;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class TrumpServiceImpl implements TrumpService {

    private static final String I = "[法宝]：";

    private List<String> getInfo() {
        String response = Api.get(ApiConstants.API_TRUMP_INDEX);

        List<String> idList = ReUtil.findAll("(?<=<a.{1,50}trainTrump.{0,80}&id=)\\S+(?=&type=0'>培育)", response, 0);

        return idList;
    }

    private void trainTrump(String id) {
        JSONObject params = JSONUtil.createObj();
        params.putOpt("id", id);
        params.putOpt("type", 0);
        String response = Api.get(ApiConstants.API_TRUMP_TRAIN, params);

        String result = ReUtil.get("开始培育!成长\\+\\d", response, 0);
        if (StrUtil.isNotBlank(result)) {
            Api.out(I, result);
        }
        result = ReUtil.get("灵气池灵气-\\d+", response, 0);
        if (StrUtil.isNotBlank(result)) {
            Api.out(I, result);
        }
        result = ReUtil.get("(?<=<br/>)\\S+\\d+品", response, 0);
        if (StrUtil.isNotBlank(result)) {
            Api.out(I, result);
        }
        result = ReUtil.get("成长:\\S+(?=<br/>)", response, 0);
        if (StrUtil.isNotBlank(result)) {
            Api.out(I, result);
        }
    }

    @Override
    public void auto() {
        Api.out(I, "开始执行");

        List<String> idList = getInfo();

        idList.forEach(id -> {
            trainTrump(id);
        });

        Api.out(I, "执行完毕");
    }

    public static void main(String[] args) {
        new TrumpServiceImpl().auto();
    }
}
